<?php

namespace addons\yunfood\library;

use addons\yunfood\model\Config;
use App\Api\PrintService;
use App\Config\YlyConfig;
use App\Oauth\YlyOauthClient;
use think\Exception;
use think\Log;
use zhongwu\protocol\RpcClient;
use addons\yunfood\library\feie\HttpClient;

class Printer
{
    /**
     * 打印内容
     */
    public static function printContent($brand, $content = '', $device = [], $throwException = true)
    {
        $platform = Config::getGroupAttributes($brand);

        $params = null;
        $result = null;
        try {
            switch ($brand) {
                case 'feie':
                    // http://help.feieyun.com/document.php
                    $time = time();
                    $params = [
                        'user'    => $platform['user'],
                        'stime'   => $time,
                        'sig'     => sha1($platform['user'] . $platform['ukey'] . $time),
                        'apiname' => 'Open_printMsg',
                        'sn'      => $device->feieData['sn'],
                        'content' => $content,
                        'times'   => $device['print_times'] ?? 1  // 打印次数
                    ];
                    $client = new HttpClient('api.feieyun.cn', 80);

                    // 打印内容
                    if ($client->post('/Api/Open/', $params)) {
                        $result = $client->getContent();
                        $result = json_decode($result, true);
                        if ($result['ret'] != 0) {
                            throw new Exception($result['msg']);
                        }
                    } else {
                        throw new Exception($client->getError());
                    }
                    break;
                case 'yilianyun':
                    // https://dev.yilianyun.net/doc/doc
                    // 获取token
                    $platform = new YlyConfig($platform['client_id'], $platform['client_secret']);
                    $client = new YlyOauthClient($platform);
                    // 开放型应用申请授权码code
                    $token = $client->getToken();
                    $print = new PrintService($token->access_token, $platform);

                    // 打印内容
                    $result = $print->index($device->yilianyunData['machine_code'], $content, time());
                    break;
                case 'zhongwu':
                    // http://www.zhongwu.co/platform.html
                    $api = 'http://api.zhongwuyun.com';
                    $rpc = new RpcClient($platform['app_id'], $platform['app_secret'], $api);
                    $printer = new \zhongwu\Printer($rpc);
                    $printer->set_args($device->zhongwuData['device_id'], $device->zhongwuData['device_secret']);

                    // 打印内容
                    $result = $printer->cloud_print($content);
                    break;
            }
        } catch (\Exception $e) {
            Log::record('打印异常：' . $e->getMessage());
            $result = $e->getMessage();
            if ($throwException) {
                throw $e;
            }
        }

        return [$params, $result];
    }

    /**
     * 绑定飞鹅打印设备
     */
    public static function bindFeieDevice($device = [])
    {
        $platformConfig = Config::getGroupAttributes('feie');
        try {
            $time = time();
            $params = [
                'user'    => $platformConfig['user'],
                'stime'   => $time,
                'sig'     => sha1($platformConfig['user'] . $platformConfig['key'] . $time),
                'apiname' => 'Open_printerAddlist',
                'content' => $device->feieData['sn'] . '#' . $device->feieData['key'] . '#' . $device['name'],
            ];
            $client = new HttpClient('api.feieyun.cn', 80);

            // 打印内容
            if ($client->post('/Api/Open/', $params)) {
                $result = $client->getContent();
                $result = json_decode($result, true);
                if ($result['ret'] != 0) {
                    throw new Exception($result['msg']);
                }
            }
            throw new Exception($client->getError());
        } catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }
    }
}
